package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.taoshunda.dao.OrderDao;
import com.baichang.taoshunda.dao.RebateDao;
import com.baichang.taoshunda.entity.Bussiness;
import com.baichang.taoshunda.entity.Order;
import com.baichang.taoshunda.entity.Rebate;
import com.baichang.taoshunda.entity.User;
import com.baichang.taoshunda.service.BusinessService;
import com.baichang.taoshunda.service.RebateService;
import com.baichang.taoshunda.service.UserService;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * @author Dylan
 * @date 2018/12/13 0013 17:57
 * @company Dylan
 * @description
 */
@Service
public class RebateServiceImpl implements RebateService {
    @Autowired
    private BusinessService businessService;
    @Autowired
    private RebateDao rebateDao;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDao orderDao;

    @Override
    public Map<String, Object> rebateCount(Integer userId) {
        Map<String, Object> map = new HashedMap();
        Integer companyCount = businessService.getRebateCount(userId);
        Integer orderCount = rebateDao.getRebateCount(userId);
        map.put("companyCount", companyCount);
        map.put("orderCount", orderCount);
        return map;
    }

    @Override
    public Map<String, Object> findRebateList(Integer userId, Integer nowPage, Integer pageSize) throws ServiceException {
        Map map = new HashMap();
        List<Map<String, Object>> list = rebateDao.findRebateList(userId, nowPage, pageSize);
        User user = userService.getUserBeanById(userId);
        int count = rebateDao.findRebateListCount(userId);
        map.put("count", count);
        map.put("rebateAmount", user.getRebateAmount());
        if (list.size() > 0) {
            map.put("list", list);
        } else {
            map.put("list", new ArrayList<>());
        }

        return map;
    }

    @Override
    public boolean cancelRebate(Integer rebateId, Integer userId) throws ServiceException {
        Rebate rebate = rebateDao.getEntityById(rebateId);
        boolean flag = rebateDao.cancelRebate(rebateId);
        if (flag) {
            User user = userService.getUserBeanById(userId);
            user.setRebateAmount(user.getRebateAmount().add(rebate.getRebateAmount()));
            userService.updateUser(user);
        }
        return flag;
    }

    @Override
    public boolean addRebate(Integer companyId, String orderNumber, Integer type) throws ServiceException {
        Bussiness company = businessService.getBusinessById(companyId);
        if (company.getUserId() != null) {
            Order order = orderDao.getOrderByOrderNumber(orderNumber);
            System.out.println("水水水水" + order.getAllMoney());
            double money = getRedpacketBanlance(order.getAllMoney().doubleValue());
            System.out.println("返回金额" + money);
            Rebate rebate = new Rebate();
            rebate.setCompanyId(companyId);
            rebate.setRebateAmount(BigDecimal.valueOf(money));
            rebate.setStatus(1);
            rebate.setType(type);
            rebate.setCreated(new Date());
            rebate.setUserId(company.getUserId());
            rebate.setOrderNumber(orderNumber);
            boolean flag = rebateDao.save(rebate) > 0;
            return flag;
        } else {
            return false;
        }

    }

    public double getRedpacketBanlance(double args) {
        int args2 = (int) args / 10 + 1;
        double args3 = args2 * 10;
        double start = (double) args3 / 1000;
        double end = start * 5;
        DecimalFormat df = new DecimalFormat("#.00");
        System.out.println("ijoijoi" + Double.valueOf(df.format(Math.random() * (end - start) + start)));
        return Double.valueOf(df.format(Math.random() * (end - start) + start));
    }
}
